define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'machine/index',
                    add_url: 'machine/add',
                    edit_url: 'machine/edit',
                    del_url: 'machine/del',
                    multi_url: 'machine/multi',
                    table: 'machine',
                }
            });

            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                pagination: false,
                commonSearch: false,
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'category', title: __('Type'), operate: false, formatter: Table.api.formatter.label},
                        {field: 'name', title: __('Name'), align: 'left', formatter:function (value, row, index) {
                                return value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');
                            }
                        },
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'price', title: __('Price'), formatter: function (value,row) {
                                return value + ' ' + row.coin_name;
                            }},
                        {field: 'stock', title: __('Stock')},
                        {field: 'c_power', title: __('Power'), formatter: function (value,row) {
                                return value + ' ' + row.power_name;
                            }},
                        {field: 'out_days', title: __('OutDays')},
                        {field: 'status', title: __('Status'), operate: false, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);

            // 为表格绑定事件
            Table.api.bindevent(table);

            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    // params.filter = JSON.stringify({type: typeStr});
                    params.type = typeStr;

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });

            //必须默认触发shown.bs.tab事件
            // $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");

        },
        category:function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'machine/category',
                    add_url: 'machine/add_cate',
                    edit_url: 'machine/edit_cate',
                    del_url: 'machine/del_cate',
                    multi_url: 'machine/multi',
                    table: 'machine_category',
                }
            });

            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                pagination: false,
                commonSearch: false,
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('CateName'), align: 'left', formatter:function (value, row, index) {
                                return value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');
                            }
                        },
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'status', title: __('Status'), operate: false, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);

            // 为表格绑定事件
            Table.api.bindevent(table);

            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    // params.filter = JSON.stringify({type: typeStr});
                    params.type = typeStr;

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });
        },
        order: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'machine/order',
                    table: 'machine_order',
                    del_url: 'machine/del_order',
                }
            });

            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                pagination: false,
                commonSearch: false,
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), formatter: function (value,row) {
                                return '<a href="javascript:void(0)" class="moneylog" data-id="' + row.id + '">' + value + '</a>';
                            }},
                        {field: 'name', title: __('Name'), align: 'left', formatter:function (value, row, index) {
                                return value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');
                            }
                        },
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'username', title: __('UserName'), formatter: function (value,row) {
                                return '<a href="javascript:void(0)" class="usernamexxx" data-id="' + row.uid + '" title="' + row.uid + '">' + value + '</a>';
                            }},
                        {field: 'price', title: __('OrderPrice'), formatter: function (value,row) {
                                return value + ' ' + row.unit;
                            }},
                        {field: 'c_power', title: __('Power')},
                        {field: 'm_start_time', title: __('StartTime'), formatter: Table.api.formatter.datetime},
                        {field: 'm_end_time', title: __('EndTime'), formatter: Table.api.formatter.datetime},
                        {field: 'run_days', title: __('RunDays')},
                        {field: 'm_income', title: __('MachineIncome')},
                        {field: 'm_status_str', title: __('Status'), formatter: function (value,row) {
                            if (row.m_status == 0) {
                                return '<a href="javascript:void(0)" class="machine-status" data-id="' + row.id + '">' + value + '</a>';
                            } else {
                                return value;
                            }
                            }},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);

            // 为表格绑定事件
            Table.api.bindevent(table);

            //跳转到扣钱记录
            $("#table").on("click",".moneylog",function (e) {
                var id = $(this).attr('data-id');
                let url = 'finance/index?from_id=' + id + '&from=buy_machine'
                Backend.api.open(url, '财务流水');
                Layer.close(index);
            })

            //查看会员信息
            $("#table").on("click",".usernamexxx",function (e) {
                var uid = $(this).attr('data-id');
                let url = 'user/user/userinfo?uid=' + uid
                Backend.api.open(url, '会员信息');
                Layer.close(index);
            })

            //封装矿机
            $("#table").on("click",".machine-status",function (e) {
                var id = $(this).attr('data-id');
                let url = 'machine/let_run?id=' + id
                Backend.api.open(url, '封存矿机',{
                    callback:function(value){
                        console.log(value)
                        Layer.close(index);
                    }});
            })

            // 绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    // params.filter = JSON.stringify({type: typeStr});
                    params.type = typeStr;

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });
        },
        add: function () {
            Controller.api.bindevent();
            setTimeout(function () {
                $("#c-type").trigger("change");
            }, 100);
        },
        add_cate: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        edit_cate: function () {
            Controller.api.bindevent();
        },
        let_run: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on("change", "#c-type", function () {
                    $("#c-pid option[data-type='all']").prop("selected", true);
                    $("#c-pid option").removeClass("hide");
                    $("#c-pid option[data-type!='" + $(this).val() + "'][data-type!='all']").addClass("hide");
                    $("#c-pid").data("selectpicker") && $("#c-pid").selectpicker("refresh");
                });

                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
